﻿using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetCoreApi.Plugins.Quartz.JobList
{
    /// <summary>
    /// 基础任务
    /// </summary>
    public abstract class BaseJob : IJob
    {
        /// <summary>
        /// 任务名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 任务
        /// </summary>
        public JobDataMap JobData { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public JobDataMap Data { get; set; }
        public async Task Execute(IJobExecutionContext context)
        {
            JobData = context.JobDetail.JobDataMap;//获取Job中的参数
            Data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数
            //定时器名称
            Name = JobData.GetString("name");
            WriteLog();//写入日志
            await Job();//执行任务
        }
        public abstract Task Job();
        private async void WriteLog()
        {
            await Task.Run(() => {
                string basePath = AppContext.BaseDirectory;
                string path = $"{basePath}logs\\quartz-log\\quartz-log-{DateTime.Now.ToString("yyyy-MM-dd")}.log";
                using (StreamWriter sw = new StreamWriter(path, true))
                {
                    sw.WriteLine(DateTime.Now.ToString($"yyyy-MM-dd HH:mm:ss.fff | {Name}"));
                }
            });
        }
    }
}
